#!usr/bin/env python  
#-*- coding:utf-8 _*-  
""" 
@author:Simon
@file: schedule01.py 
@time: 2017/11/{DAY} 
"""

import time,sched,os

# 定义函数
def worker(msg, starttime):
    print("任务执行的时刻", parse_time(time.time()), "传达的消息是", msg, '任务建立时刻', parse_time(starttime))

def perform_command(cmd):
    os.system(cmd)

def parse_time(timestamp):
    # 转换成localtime
    time_local = time.localtime(timestamp)
    # 转换成新的时间格式(2016-05-05 20:28:54)
    return time.strftime("%Y-%m-%d %H:%M:%S", time_local)


# 主控程序
print('程序启动时刻：', parse_time(time.time()))
# 任务调度器
sch = sched.scheduler(time.time, time.sleep)
# 加入任务
sch.enter(1, 1, worker, ("hello", time.time()))
sch.enter(116, 1, worker, ("world", time.time()))
# 运行任务
sch.run()

print('睡眠２秒前时刻：', parse_time(time.time()))
time.sleep(2)
print('睡眠２秒结束时刻：', parse_time(time.time()))
